/* 
 * Serenity ENB
 * By WLHM15
*/
bool bUseBlur <
	string UIName="Aktifkan Radial Blur";
> = {true};

bool bUseRaw <
	string UIName="Aktifkan Kedalaman";
> = {true};

bool bUseVign <
	string UIName="Aktifkan Vignette";
> = {true};


#define FXAA	0


#define fVigAmount			0.5
#define fVigCurve			0.5
#define fVigRadius			1.2
#define fVigColor			float3(0.0,0.0,0.0)
 
#define fxaa_Subpix               0.000  //[0.000 to 1.000] Choose the amount of sub-pixel aliasing removal. Higher values makes the image softer/blurrier.
#define fxaa_EdgeThreshold        0.0001  //[0.000 to 1.000] Edge detection threshold. The minimum amount of local contrast required to apply algorithm. Similar to SMAA_THRESHOLD
#define fxaa_EdgeThresholdMin     0.000  //[0.000 to 1.000] Darkness threshold. Pixels darker than this are not processed in order to increase performance.

float4 tempF1;
float4 tempF2;
float4 tempF3;
float4 Timer;
float4 ScreenSize;
float ENightDayFactor;
	
//////////////////////////////////////

texture2D texColor;
texture2D texNoise;
texture2D texDepth;

//////////////////////////////////////

sampler2D SamplerColor = sampler_state
{
	Texture   = <texColor>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = NONE;//NONE;
	AddressU  = Clamp;
	AddressV  = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

sampler2D SamplerDepth = sampler_state
{
	Texture = <texDepth>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = NONE;
	AddressU = Clamp;
	AddressV = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

sampler2D SamplerNoise = sampler_state
{
	Texture   = <texNoise>;
	MinFilter = POINT;
	MagFilter = POINT;
	MipFilter = NONE;//NONE;
	AddressU  = Wrap;
	AddressV  = Wrap;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

//////////////////////////////////////
 
struct VS_OUTPUT_POST 
{
	float4 vpos  : POSITION;
	float2 texcoord : TEXCOORD0;
};

struct VS_INPUT_POST 
{
	float3 pos  : POSITION;
	float2 texcoord : TEXCOORD0;
};

//////////////////////////////////////
VS_OUTPUT_POST VS_PostProcess(VS_INPUT_POST IN)
{
	VS_OUTPUT_POST OUT;

	float4 pos=float4(IN.pos.x,IN.pos.y,IN.pos.z,1.0);

	OUT.vpos=pos;
	OUT.texcoord.xy=IN.texcoord.xy;

	return OUT;
}
//////////////////////////////////////
#include "Fxaa311.h"

//////////////////////////////////////
float4 PS_AntiAliasing(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{
	float2 PixelSize = float2(ScreenSize.y,ScreenSize.y*ScreenSize.z);
	return FxaaPixelShader(IN.texcoord, SamplerColor, PixelSize, float4(0.0f, 0.0f, 0.0f, 0.0f), fxaa_Subpix, fxaa_EdgeThreshold, fxaa_EdgeThresholdMin);
}

float4 PS_PostProcess(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{
	float4 warna = tex2D(SamplerColor, IN.texcoord.xy);
	
	if(bUseBlur == true)
	{
		float b = 0.0;
		float2 uv = IN.texcoord.xy;
  
		for (int i = 1; i <= 20; i++)
		{ 
			uv -= b;
			uv *= 1.002;
			b = (1-(1*pow(abs(1.002), i))) / 2;
			uv += b;
			warna += tex2D(SamplerColor, uv.xy);	
		}
		warna.xyz = warna / (20 + 1);
	}
		
	if(bUseVign == true)
	{
		float2 uiv = (IN.texcoord.xy - 0.5) * fVigRadius;
		float vignette = saturate(dot(uiv.xy, uiv.xy));
		vignette = pow(vignette, fVigCurve);
		warna.rgb = lerp(warna.rgb, fVigColor, vignette * fVigAmount);
	}
	
	if(bUseRaw == true)
	{
		float dalam = tex2D(SamplerDepth, IN.texcoord.xy).r;
		dalam = 1.0 / (-99.0 * dalam + 101.0);
		warna = dalam;
	}
	
	float dither_bit  = 8.0;
	float sine = sin(dot(IN.texcoord, float2(12.9898,78.233)));
	float noise = frac(sine * 43758.5453 + IN.texcoord.x);
	float dither_shift = (1.0 / (pow(2,dither_bit) - 1.0));
	float dither_shift_half = (dither_shift * 0.5);
	dither_shift = dither_shift * noise - dither_shift_half;
	warna.rgb += float3(-dither_shift, dither_shift, -dither_shift);
	
	return warna;
}
//////////////////////////////////////
technique PostProcess
{
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 PS_AntiAliasing();
	}
	
}

technique PostProcess2
{
	pass P0
	{
		VertexShader = compile vs_3_0 VS_PostProcess();
		PixelShader  = compile ps_3_0 PS_PostProcess();
	}
}